﻿using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class UploadCheckPresenterTests : BasePresenterTests
    {
        private Administrator _admin;
        private Mock<IUploadCheckView> _viewMock;
        
        private void Prepare()
        {
            _admin = new Administrator();
            Site site = new Site();
            _admin.CurrentSite = site;
            MediaFolder mediaFolder = new MediaFolder();
            MediaFile fileA = new MediaFile { Name = "fileA.gif" };
            mediaFolder.Files.Add(fileA);
            MediaFile fileB = new MediaFile { Name = "file-B.pdf" };
            mediaFolder.Files.Add(fileB);
            MediaFile fileC = new MediaFile { Name = "fileC.htm" };
            site.MediaFiles.Add(fileC);
            MediaFile fileD = new MediaFile { Name = "fileD.jpg" };
            site.MediaFiles.Add(fileD);
            InitRepositoryFactoryMock();
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            MediaRepositoryMock.Setup(m => m.GetMediaFolder(1)).Returns(mediaFolder);
            _viewMock = new Mock<IUploadCheckView>();
            _viewMock.SetupAllProperties();
            _viewMock.Setup(v => v.AdministratorID).Returns(1);
        }
        
        [TestMethod]
        public void CheckFiles_SelectedFilesExistInMediaFolder_Warn()
        {
            Prepare();
            _admin.SelectedMediaFolderID = 1;
            UploadCheckPresenter presenter = new UploadCheckPresenter(_viewMock.Object, FactoryMock.Object);
            const string selectedFiles = "fileA.gif,file-B.pdf";
            presenter.CheckFiles(selectedFiles);
            _viewMock.Verify(o => o.SetExistingFiles(It.Is<string>(v => v == "fileA.gif, file-B.pdf")));
        }
        
        [TestMethod]
        public void CheckFiles_SelectedFilesExistInRoot_Warn()
        {
            Prepare();
            _admin.SelectedMediaFolderID = -1; // Root
            UploadCheckPresenter presenter = new UploadCheckPresenter(_viewMock.Object, FactoryMock.Object);
            const string selectedFiles = "fileC.htm,fileD.jpg";
            presenter.CheckFiles(selectedFiles);
            _viewMock.Verify(o => o.SetExistingFiles(It.Is<string>(v => v == "fileC.htm, fileD.jpg")));
        }
        
        [TestMethod]
        public void CheckFiles_SelectedFilesDontExist_DontWarn()
        {
            Prepare();
            _admin.SelectedMediaFolderID = 1;
            UploadCheckPresenter presenter = new UploadCheckPresenter(_viewMock.Object, FactoryMock.Object);
            const string selectedFiles = "filexxxxxA.gif,fileeeeeeeeeeeeee-B.pdf";
            presenter.CheckFiles(selectedFiles);
            _viewMock.Verify(o => o.SetExistingFiles(It.Is<string>(v => v == ""))); // Results in no warning
        }

        [TestMethod]
        public void CheckFiles_SelectedFileExistsAndHasSpacesInTheName_SpacesAreReplacedWithHyphensDuringCheckAndWarningIsIssued()
        {
            Prepare();
            _admin.SelectedMediaFolderID = 1;
            UploadCheckPresenter presenter = new UploadCheckPresenter(_viewMock.Object, FactoryMock.Object);
            const string selectedFiles = "file B.pdf";
            presenter.CheckFiles(selectedFiles);
            _viewMock.Verify(o => o.SetExistingFiles(It.Is<string>(v => v == "file B.pdf"))); // Results in no warning
        }
    }
}
